package JsonInterface;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import HibernateFacet.second.DeviceData;
import ImplementInterface.Ad2ServiceImpl;
import JsonInterface.CRC16;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;


public class SocketThread extends Thread {
    public Socket socket;
    public DataInputStream in;
    public DataOutputStream out;
    //	public DataOutputStream outgoing;
    public byte[] data = new byte[1024];
    public boolean onsubmi = false;
    public int isSocketClose = 0;
    public Ad2 ad2;  //接口
    public int tcpShortConnectCount = 0;
    public  String str = "";
    public int count = 0;
    public int endFlag = 0;
    public static HashMap<String, Socket> socketMap = new HashMap<>();

    public SocketThread(Socket socket) {
        this.socket = socket;
        try {
            in = new DataInputStream(this.socket.getInputStream());
            out = new DataOutputStream(this.socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //@Override
    public void run() {

        while (true) {

            System.out.println("waitting for the next Receiving....");

            int i = 0;
            try {
                //System.out.println("FROM:" + in.getAddress().getHostAddress() + "PORT:" + recvPacket.getPort() + " LENGTH:" + recvPacket.getLength());
                System.out.println("ZKP51-63H2客户端连接: " + this.socket.getInetAddress().getHostAddress() + ":" + this.socket.getPort());
                i = in.read(data);
                System.out.println("count:" + i);
                for (int j = 0; j < i; j++) {
                    System.out.print(data[j] + " ");
                }
                System.out.println();
                for (int k = 0; k < i; k++) {
                    System.out.print((Integer.toHexString(data[k] & 0xff)) + " ");
                }
                System.out.println(" ");
                if (i == -1) {
                    out.close();
                    Ad2.socketMap2.remove(str);
                    System.out.println(str+  "已经断开连接");
                    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
                    Ad2ServiceImpl ad2ServiceImpl = (Ad2ServiceImpl) context.getBean("Ad2ServiceImpl");
                    ad2ServiceImpl.clearedSumPower(str); //当前功率归零
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("break...");

                break;
            }
            if (i == 45 && (data[12] & 0xff) == 0x03) {
                int crc = CRC16.getCRC1021(data, i - 2);
                if ((crc >> 8 & 0xff) == ((int) data[i - 2] & 0xff) && ((crc & 0xff) == ((int) data[i - 1] & 0xff))) {
                    int d1 = ((int) data[0] & 0xff) - 0x30;
                    int d2 = ((int) data[1] & 0xff) - 0x30;
                    int d3 = ((int) data[2] & 0xff) - 0x30;
                    int d4 = ((int) data[3] & 0xff) - 0x30;
                    int d5 = ((int) data[4] & 0xff) - 0x30;
                    int d6 = ((int) data[5] & 0xff) - 0x30;
                    int d7 = ((int) data[6] & 0xff) - 0x30;
                    int d8 = ((int) data[7] & 0xff) - 0x30;
                    int d9 = ((int) data[8] & 0xff) - 0x30;
                    int d10 = ((int) data[9] & 0xff) - 0x30;
                    int d11 = ((int) data[10] & 0xff) - 0x30;
                    int d12 = ((int) data[11] & 0xff) - 0x30;
                    str = d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 + "" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + d10 + "" + d11 + "" + d12;
                    System.out.println("hhhhhhhhhhhhh str=" + str);
                    if (Ad2.socketMap2.containsKey(str)) {
                        Ad2.socketMap2.remove(str);
                        Ad2.socketMap2.put(str, this.socket);

                    } else {
                        Ad2.socketMap2.put(str, this.socket);
                    }
                    for (Map.Entry<String, Socket> entry : socketMap.entrySet()) {
                        System.out.println("socketnum-----" + entry.getKey());
                    }
                    System.out.println("客户端连接: " + this.socket.getInetAddress().getHostAddress() + ":" + this.socket.getPort());
                    Map map = new HashMap<String, Object>();
                    map.put("IP", this.socket.getInetAddress().getHostAddress());
                    map.put("PORT", this.socket.getPort());
                    map.put("address", str);

                    if (Ad2.list1.size() == 0) {
                        Ad2.list1.add(map);
                    } else {
                        for (int i2 = 0; i2 < Ad2.list1.size(); i2++) {
                            Map mapx = (Map) Ad2.list1.get(i2);
                            if (mapx.get("address").equals(map.get("address"))) {
                                Ad2.list1.remove(i2);
                            }
                        }
                        Ad2.list1.add(map);
                    }

                   /* byte[] buf = new byte[]{data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], (byte) 0x10, (byte) 0x01, (byte) 0x20, 0, 0};
//		System.out.println("controlZKP51 3");
                    int crc2 = CRC16.getCRC1021(buf, buf.length - 2);
                    int ch = (int) (crc >> 8) & 0xff;
                    int cl = (int) crc & 0xff;
                    buf[buf.length - 2] = (byte) ch;
                    buf[buf.length - 1] = (byte) cl;
                    String ip = map.get("IP").toString();
                    int port = map.get("PORT").hashCode();
//		          System.out.println(map.get("IP").hashCode());
                    System.out.println(map.get("PORT").hashCode());
                    SocketAddress sk = new InetSocketAddress(ip, port);
                    DatagramPacket packet = new DatagramPacket(buf, buf.length, sk);*/
                    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
                    Ad2ServiceImpl ad2ServiceImpl = (Ad2ServiceImpl) context.getBean("Ad2ServiceImpl");
                    ad2ServiceImpl.addDeviceData(str,out, data);

                }


            }
            if (i == 18 && (data[12] & 0xff) == 0x04) {
                int crc = CRC16.getCRC1021(data, i - 2);
                if ((crc >> 8 & 0xff) == ((int) data[i - 2] & 0xff) && ((crc & 0xff) == ((int) data[i - 1] & 0xff))) {
                    int d1 = ((int) data[0] & 0xff) - 0x30;
                    int d2 = ((int) data[1] & 0xff) - 0x30;
                    int d3 = ((int) data[2] & 0xff) - 0x30;
                    int d4 = ((int) data[3] & 0xff) - 0x30;
                    int d5 = ((int) data[4] & 0xff) - 0x30;
                    int d6 = ((int) data[5] & 0xff) - 0x30;
                    int d7 = ((int) data[6] & 0xff) - 0x30;
                    int d8 = ((int) data[7] & 0xff) - 0x30;
                    int d9 = ((int) data[8] & 0xff) - 0x30;
                    int d10 = ((int) data[9] & 0xff) - 0x30;
                    int d11 = ((int) data[10] & 0xff) - 0x30;
                    int d12 = ((int) data[11] & 0xff) - 0x30;
                    str = d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 + "" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + d10 + "" + d11 + "" + d12;
                    System.out.println("hhh2 str=" + str);
                    if (socketMap.containsKey(str)) {
                        socketMap.remove(str);
                        socketMap.put(str, this.socket);
                    } else {
                        socketMap.put(str, this.socket);
                    }
                    for (Map.Entry<String, Socket> entry : socketMap.entrySet()) {
                        System.out.println("socketnum-----" + entry.getKey());
                    }
                    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
                    Ad2ServiceImpl ad2ServiceImpl = (Ad2ServiceImpl) context.getBean("Ad2ServiceImpl");
                    ad2ServiceImpl.addDevice(str,out, data);
                }
            }
           /* if (i == 17 && (data[12] & 0xff) == 0x10) {
                int crc = CRC16.getCRC1021(data, i - 2);
                if ((crc >> 8 & 0xff) == ((int) data[i - 2] & 0xff) && ((crc & 0xff) == ((int) data[i - 1] & 0xff))) {
                    int d1 = ((int) data[0] & 0xff) - 0x30;
                    int d2 = ((int) data[1] & 0xff) - 0x30;
                    int d3 = ((int) data[2] & 0xff) - 0x30;
                    int d4 = ((int) data[3] & 0xff) - 0x30;
                    int d5 = ((int) data[4] & 0xff) - 0x30;
                    int d6 = ((int) data[5] & 0xff) - 0x30;
                    int d7 = ((int) data[6] & 0xff) - 0x30;
                    int d8 = ((int) data[7] & 0xff) - 0x30;
                    int d9 = ((int) data[8] & 0xff) - 0x30;
                    int d10 = ((int) data[9] & 0xff) - 0x30;
                    int d11 = ((int) data[10] & 0xff) - 0x30;
                    int d12 = ((int) data[11] & 0xff) - 0x30;
                    str = d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 + "" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + d10 + "" + d11 + "" + d12;
                    System.out.println("hhh2 str=" + str);
                    if (socketMap.containsKey(str)) {
                        socketMap.remove(str);
                        socketMap.put(str, this.socket);
                    } else {
                        socketMap.put(str, this.socket);
                    }
                    for (Map.Entry<String, Socket> entry : socketMap.entrySet()) {
                        System.out.println("socketnum-----" + entry.getKey());
                    }
                    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
                    Ad2ServiceImpl ad2ServiceImpl = (Ad2ServiceImpl) context.getBean("Ad2ServiceImpl");
                    ad2ServiceImpl.updateCondition(str,out, data);
                }
            }*/
            if (i == 3) {
                System.out.println("okokokok");
                Ad2.resMap.put(str,data);
                /*Ad2.responseBuf = data;*/
                /*if ((data[0] & 0xff) == (0x06 & 0xff) && (data[1] & 0xff) == (0x4F & 0xff) && (data[2] & 0xff) == (0x4B & 0xff)) {
                    System.out.println("okokokok");
                    Ad2.responseBuf = data;
                }*/
            }
        }
    }
}//end
